/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2025 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.itemgroup.groups

import net.ccbluex.fastutil.mapToArray
import net.ccbluex.liquidbounce.features.itemgroup.ClientItemGroup
import net.ccbluex.liquidbounce.utils.client.asPlainText
import net.ccbluex.liquidbounce.utils.client.textOf
import net.ccbluex.liquidbounce.utils.item.createSplashPotion
import net.ccbluex.liquidbounce.utils.kotlin.toTypedArray
import net.minecraft.block.Blocks
import net.minecraft.component.ComponentChanges
import net.minecraft.component.DataComponentTypes
import net.minecraft.component.type.BlocksAttacksComponent
import net.minecraft.entity.effect.StatusEffectInstance
import net.minecraft.entity.effect.StatusEffects
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.registry.Registries
import net.minecraft.registry.tag.ItemTags
import java.util.Optional

/**
 * https://github.com/ViaVersion/ViaVersion/blob/081fc0db41a06faa4465402ec25c794392539f2a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_4to1_21_5/rewriter/BlockItemPacketRewriter1_21_5.java#L514
 */
private val OLD_SWORD_BLOCKING_COMPONENT = BlocksAttacksComponent(
    0f,
    0f,
    listOf(
        BlocksAttacksComponent.DamageReduction(
            90f,
            Optional.empty(),
            -0.5f,
            0.5f
        )
    ),
    BlocksAttacksComponent.ItemDamage(0f, 0f, 0f),
    Optional.empty(),
    Optional.empty(),
    Optional.empty(),
)

class ExploitsItemGroup : ClientItemGroup(
    "Exploits",
    icon = { Items.LINGERING_POTION.defaultStack },
    items = {
        // Old swords
        val oldSwords = Registries.ITEM.streamEntries().filter { item ->
            item.isIn(ItemTags.SWORDS)
        }.map { item ->
            ItemStack(
                item,
                1,
                ComponentChanges.builder()
                    .add(DataComponentTypes.BLOCKS_ATTACKS, OLD_SWORD_BLOCKING_COMPONENT)
                    .add(DataComponentTypes.CUSTOM_NAME, textOf("1.8.9 ".asPlainText(), item.value().name))
                    .build(),
            )
        }.toList()

        it.addAll(oldSwords)

        // Add unobtainable items
        it.add(ItemStack(Blocks.COMMAND_BLOCK))
        it.add(ItemStack(Blocks.CHAIN_COMMAND_BLOCK))
        it.add(ItemStack(Blocks.REPEATING_COMMAND_BLOCK))
        it.add(ItemStack(Items.COMMAND_BLOCK_MINECART))
        it.add(ItemStack(Blocks.END_PORTAL_FRAME))
        it.add(ItemStack(Blocks.DRAGON_EGG))
        it.add(ItemStack(Blocks.BARRIER))
        it.add(ItemStack(Blocks.JIGSAW))
        it.add(ItemStack(Blocks.STRUCTURE_BLOCK))
        it.add(ItemStack(Blocks.STRUCTURE_VOID))
        it.add(ItemStack(Blocks.SPAWNER))
        it.add(ItemStack(Items.DEBUG_STICK))

        // Add unobtainable potions
        it.add(createSplashPotion("Troll Potion",
            effects = Registries.STATUS_EFFECT.streamEntries().toTypedArray()
                .mapToArray { statusEffect ->
                    StatusEffectInstance(statusEffect, Int.MAX_VALUE, 127)
                })
        )

        it.add(createSplashPotion("Kill Potion",
            StatusEffectInstance(StatusEffects.INSTANT_HEALTH, 0, 125),
            StatusEffectInstance(StatusEffects.INSTANT_DAMAGE, 0, 125)
        ))

    }
)
